<template>
  <view class="container">
    <Table ref="table" :fetchData="fetchData" :autoFetch="false">
      <template #default="{ data }">
        <uni-card
          @click="cardClick(item)"
          v-for="item in data"
          :title="item.pointName"
          :extra="item.checkState"
          :sub-title="item.checkInfo"
        >
        </uni-card>
      </template>
    </Table>
    <button
      @click="saoyisao"
      v-if="planTaskState != '已超时' && planTaskState != '已完成'"
      type="primary"
      class="saoyisao"
    >
      扫一扫
    </button>
  </view>
</template>
<script>
export default {
  data() {
    return {
      pid: "",
      state: "",
    };
  },
  onLoad(payload) {
    this.pid = payload.pid;
    this.state = payload.planTaskState;
  },
};
</script>
<script setup>
import { ref, getCurrentInstance, onMounted, reactive } from "vue";
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import Table from "@/components/uniList.vue";
import { useAsyncData } from "@/hooks/useAsyncData.js";
import { taskPoints } from "@/api/znxj/xjrw.js";
import { router } from "@/router/index.js";

const cardClick = (item) => {
  // router.toPage({
  //   path: "/pages/znxj/xjrw/components/input",
  //   params: {
  //     row: JSON.stringify(item),
  //   },
  // });
};
const table = ref();
const filterForm = reactive({
  planTaskId: null,
  pageSize: 9999,
});
const { fetchData, getList, queryParams } = useAsyncData({
  table,
  APIS: taskPoints,
  filterForm,
});
const points = ref([]);
const planTaskState = ref("");
onShow(async () => {
  const { pid, state } = getCurrentInstance().data;
  filterForm.planTaskId = pid;
  points.value = await getList();
  planTaskState.value = state;
});
onMounted(async () => {
  const { pid, state } = getCurrentInstance().data;
  filterForm.planTaskId = pid;

  points.value = await getList();
  planTaskState.value = state;
});

const saoyisao = () => {
  // 只允许通过相机扫码
  uni.scanCode({
    // onlyFromCamera: true,
    success: function (res) {
      let pointId = "";
      try {
        let data = JSON.parse(res.result);
        if (data instanceof Object && data.type == "巡检") {
          pointId = data.id;
        }
      } catch (e) {
        pointId = res.result;
      }
      const node = points.value.find((item) => item.pointId == pointId);
      // const node = points.value.find(item => item.pointId == 14)
      if (node) {
        router.toPage({
          path: "/pages/znxj/xjrw/components/input",
          params: {
            row: JSON.stringify(node),
          },
        });
      } else {
        uni.showToast({
          title: "该点位不在任务中或二维码错误",
        });
      }
    },
  });
};
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 12vw;
}
.saoyisao {
  position: fixed;
  bottom: 0;
  width: 100vw;
  height: 12vw;
  left: 0;
  right: 0;
}
</style>
